package com.springboot.config;

import com.springboot.service.impl.UserDetailSecurityImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author gali
 * @description:权限校验
 * @date 2022/6/22 15:31
 */
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true) //开启全局方法权限
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private UserDetailSecurityImpl userDetailSecurity;

    /**
     * 指定加密方式
     * @return
     */
    @Bean
    public PasswordEncoder passwordEncoder(){
        return  new BCryptPasswordEncoder();
    }
    public void configure(AuthenticationManagerBuilder auth) throws Exception{
        auth.userDetailsService(userDetailSecurity)
                .passwordEncoder(passwordEncoder());
    }
    public void configure(HttpSecurity http) throws Exception{
        http.authorizeRequests()
                .antMatchers(HttpMethod.POST,"/user/addUser") //该权限不过滤
                .permitAll()//其他方法都要过滤
                .anyRequest()
                .authenticated()
                .and()
                .formLogin()
                .and()
                .csrf()//网络攻击
                .disable();
    }
}
